package com.group.app.example.repository;

import com.group.app.example.domain.FieldNodeModel;
import com.group.app.example.vo.SearchVo;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class FieldRepository {

    @Autowired
    @Qualifier("pgJdbcTemplate")
    private JdbcTemplate pgjdbcTemplate;

    String baseSql="select\n" +
            "*\n" +
            "from(\n" +
            "select\n" +
            "arr[2] as first,\n" +
            "arr[3] as second,\n" +
            "arr[4] as third,\n" +
            "field_name,\n" +
            "field_type\n" +
            "from\n" +
            "field_station fs\n" +
            "INNER JOIN\n" +
            "(select id,string_to_array(parent_link,'>')arr from field_node ) fwn\n" +
            "on fwn.id=fs.id)a\n" +
            "where 1=1";


    public Map<String,Object> queryLabelInfo1(SearchVo searchVo,int limit,int offset){
        String var="count(*)\n";
        List<Object> dataList=new ArrayList<>();
        String conditionSql = conditionSql(baseSql, null, null, searchVo.getFieldName(), searchVo.getFieldType(), dataList);
        Map<String, Object> totalMap = pgjdbcTemplate.queryForMap(conditionSql.replace("*\n", var), dataList.toArray());
        conditionSql+="ORDER BY first LIMIT ? OFFSET ?";
        dataList.add(limit);
        dataList.add(offset);
        List<Map<String, Object>> listMap = pgjdbcTemplate.queryForList(conditionSql,dataList.toArray());
        totalMap.put("list",listMap);
        return  totalMap;
    }

    public List<Map<String,Object>> allLabelInfoByCondition(SearchVo searchVo){
        List<Object> dataList=new ArrayList<>();
        String conditionSql = conditionSql(baseSql, null, null, searchVo.getFieldName(), searchVo.getFieldType(), dataList);
        List<Map<String, Object>> listMap = pgjdbcTemplate.queryForList(conditionSql,dataList.toArray());
        return  listMap;
    }

    private String conditionSql(String initialSql,
                                String beginDate,
                                String endDate,
                                String fieldName,
                                String fieldType,
                                List<Object> initList){
        if (Strings.isNotBlank(beginDate)){
            initialSql +="AND to_char(update_time,'yyyy-MM-dd') >= ?";
            initList.add(beginDate);
        }
        if (Strings.isNotBlank(endDate)){
            initialSql +="AND to_char(update_time,'yyyy-MM-dd') <= ?";
            initList.add(endDate);
        }
        if (Strings.isNotBlank(endDate)){
            initialSql +="AND to_char(update_time,'yyyy-MM-dd') >= ?";
            initList.add(endDate);
        }
        if (Strings.isNotBlank(fieldName)){
            initialSql +="AND field_name like ?";
            initList.add("%"+fieldName+"%");
        }
        if (Strings.isNotBlank(fieldType)){
            initialSql +="AND field_type like ?";
            initList.add("%"+fieldType+"%");
        }
        return initialSql;
    }



}
